<?php

class SlideshowController extends Controller {

  public $content;

  public function init() {

    if (user()->getUserProperty('username') == '')
      $this->redirect(FRONT_SITE_URL);
  }

  public function actionIndex() {
    Yii::app()->end();
  }

  public function actionInitialize(){
    $result = array();
    $offset = 3;
    $find_gender = 'male';
    $mProfile = new mProfile();
    $profile = $mProfile->getProfile(user()->getUserProperty('user_id'));
    if($profile['gender']=='male')
      $find_gender = 'female';
    
    $mUser = new User();
    $mUserCur = $mUser->getUserToViewed($offset, $find_gender);
    if($mUserCur==false){
      echo CJSON::encode($result);
      Yii::app()->end();
    }
    $link_img = '';
    if ($mUserCur['link_img'] != null) 
        $link_img = FRONT_SITE_URL.'www/uploads/photos/thumbs/thumb_'.$mUserCur['link_img']; 
    else {
        $link_img = $mUserCur['gender'] == 'male' ? FRONT_SITE_URL . 'www/images/images1/noPhoto-1.gif' : FRONT_SITE_URL . 'www/images/images1/noPhoto-2.gif';
    }
    
    $mUserCur['PhotoUrl'] = $link_img;
    $mUserCur['ProfileUrl'] = FRONT_SITE_URL.'user/profile/viewprofile?profile='.$mUserCur['user_id'];
    $mUserCur['ImInterestedUrl'] = FRONT_SITE_URL.'/v3/ImInterested/?profile='.$mUserCur['user_id'];
    $favorite = mUserFavorite::model()->find('user_id=:user_id and user_favorited=:user_favorited', 
                                        array(':user_id'=>user()->getUserProperty('user_id'),'user_favorited'=>$mUserCur['user_id']));
    if($favorite != false)
      $favorite = true;
    
    $like = mUserLike::model()->find('user_id=:user_id and user_id_liked=:user_id_liked', 
                                        array(':user_id'=>user()->getUserProperty('user_id'),'user_id_liked'=>$mUserCur['user_id']));
    if($like != false)
      $like = true;
    
    $mUserCur['IsFavorite'] = $favorite;
    $mUserCur['MediaId'] = '4855284';
    $mUserCur['IsLiked'] = $like;
    $mUserCur['EncodedMemberId'] = $mUserCur['user_id'];
    $mUserCur['SiteId'] = '21';    
    
    $result['CurrentMember'] = $mUserCur;
    
    
        
    $mUserCur = $mUser->getUserToViewed($offset-1, $find_gender);
    if($mUserCur==false){
      echo CJSON::encode($result);
      Yii::app()->end();
    }
    $link_img = '';
    if ($mUserCur['link_img'] != null) 
        $link_img = FRONT_SITE_URL.'www/uploads/photos/thumbs/thumb_'.$mUserCur['link_img']; 
    else {
        $link_img = $mUserCur['gender'] == 'male' ? FRONT_SITE_URL . 'www/images/images1/noPhoto-1.gif' : FRONT_SITE_URL . 'www/images/images1/noPhoto-2.gif';
    }
    
    $mUserCur['PhotoUrl'] = $link_img;
    $mUserCur['ProfileUrl'] = FRONT_SITE_URL.'user/profile/viewprofile?profile='.$mUserCur['user_id'];
    $mUserCur['ImInterestedUrl'] = FRONT_SITE_URL.'/v3/ImInterested/?profile='.$mUserCur['user_id'];
    $favorite = mUserFavorite::model()->find('user_id=:user_id and user_favorited=:user_favorited', 
                                        array(':user_id'=>user()->getUserProperty('user_id'),'user_favorited'=>$mUserCur['user_id']));
    if($favorite != false)
      $favorite = true;
    
    $like = mUserLike::model()->find('user_id=:user_id and user_id_liked=:user_id_liked', 
                                        array(':user_id'=>user()->getUserProperty('user_id'),'user_id_liked'=>$mUserCur['user_id']));
    if($like != false)
      $like = true;
    
    $mUserCur['IsFavorite'] = $favorite;
    $mUserCur['MediaId'] = '4855284';
    $mUserCur['IsLiked'] = $like;
    $mUserCur['EncodedMemberId'] = $mUserCur['user_id'];
    $mUserCur['SiteId'] = '21';    
    $result['PreviousMember'] = $mUserCur;
    
    
    $mUserCur = $mUser->getUserToViewed($offset+1, $find_gender);
    if($mUserCur==false){
      echo CJSON::encode($result);
      Yii::app()->end();
    }
    $link_img = '';
    if ($mUserCur['link_img'] != null) 
        $link_img = FRONT_SITE_URL.'www/uploads/photos/thumbs/thumb_'.$mUserCur['link_img']; 
    else {
        $link_img = $mUserCur['gender'] == 'male' ? FRONT_SITE_URL . 'www/images/images1/noPhoto-1.gif' : FRONT_SITE_URL . 'www/images/images1/noPhoto-2.gif';
    }
    
    $mUserCur['PhotoUrl'] = $link_img;
    $mUserCur['ProfileUrl'] = FRONT_SITE_URL.'user/profile/viewprofile?profile='.$mUserCur['user_id'];
    $mUserCur['ImInterestedUrl'] = FRONT_SITE_URL.'/v3/ImInterested/?profile='.$mUserCur['user_id'];
    $favorite = mUserFavorite::model()->find('user_id=:user_id and user_favorited=:user_favorited', 
                                        array(':user_id'=>user()->getUserProperty('user_id'),'user_favorited'=>$mUserCur['user_id']));
    if($favorite != false)
      $favorite = true;
    
    $like = mUserLike::model()->find('user_id=:user_id and user_id_liked=:user_id_liked', 
                                        array(':user_id'=>user()->getUserProperty('user_id'),'user_id_liked'=>$mUserCur['user_id']));
    if($like != false)
      $like = true;
    
    $mUserCur['IsFavorite'] = $favorite;
    $mUserCur['MediaId'] = '4855284';
    $mUserCur['IsLiked'] = $like;
    $mUserCur['EncodedMemberId'] = $mUserCur['user_id'];
    $mUserCur['SiteId'] = '21';    
    
    $result['NextMember'] = $mUserCur;
            
    $result['Status'] = 'Success';
    $result['Offset'] = $offset;
    echo CJSON::encode($result);
    Yii::app()->end();
  }
  
  public function actionPrevious(){
    $result = array();
    if(!isset($_POST['offset'])){
      echo CJSON::encode($result);
      Yii::app()->end();
    }
    
    $find_gender = 'male';
    $mProfile = new mProfile();
    $profile = $mProfile->getProfile(user()->getUserProperty('user_id'));
    if($profile['gender']=='male')
      $find_gender = 'female';
    
    $mUser = new User();
    $mUserCur = $mUser->getUserToViewed($_POST['offset']-1, $find_gender);
    if($mUserCur==false){
      echo CJSON::encode($result);
      Yii::app()->end();
    }
    $link_img = '';
    if ($mUserCur['link_img'] != null) 
        $link_img = FRONT_SITE_URL.'www/uploads/photos/thumbs/thumb_'.$mUserCur['link_img']; 
    else {
        $link_img = $mUserCur['gender'] == 'male' ? FRONT_SITE_URL . 'www/images/images1/noPhoto-1.gif' : FRONT_SITE_URL . 'www/images/images1/noPhoto-2.gif';
    }
    
    $mUserCur['PhotoUrl'] = $link_img;
    $mUserCur['ProfileUrl'] = FRONT_SITE_URL.'user/profile/viewprofile?profile='.$mUserCur['user_id'];
    $mUserCur['ImInterestedUrl'] = FRONT_SITE_URL.'/v3/ImInterested/?profile='.$mUserCur['user_id'];
    $favorite = mUserFavorite::model()->find('user_id=:user_id and user_favorited=:user_favorited', 
                                        array(':user_id'=>user()->getUserProperty('user_id'),'user_favorited'=>$mUserCur['user_id']));
    if($favorite != false)
      $favorite = true;
    
    $like = mUserLike::model()->find('user_id=:user_id and user_id_liked=:user_id_liked', 
                                        array(':user_id'=>user()->getUserProperty('user_id'),'user_id_liked'=>$mUserCur['user_id']));
    if($like != false)
      $like = true;
    
    $mUserCur['IsFavorite'] = $favorite;
    $mUserCur['MediaId'] = '4855284';
    $mUserCur['IsLiked'] = $like;
    $mUserCur['EncodedMemberId'] = $mUserCur['user_id'];
    $mUserCur['SiteId'] = '21';    
    
    $result['CurrentMember'] = $mUserCur;
    
    
    $result['NextMember'] = null;
    
    
    $mUserCur = $mUser->getUserToViewed($_POST['offset']-2, $find_gender);
    if($mUserCur==false){
      echo CJSON::encode($result);
      Yii::app()->end();
    }
    $link_img = '';
    if ($mUserCur['link_img'] != null) 
        $link_img = FRONT_SITE_URL.'www/uploads/photos/thumbs/thumb_'.$mUserCur['link_img']; 
    else {
        $link_img = $mUserCur['gender'] == 'male' ? FRONT_SITE_URL . 'www/images/images1/noPhoto-1.gif' : FRONT_SITE_URL . 'www/images/images1/noPhoto-2.gif';
    }
    
    $mUserCur['PhotoUrl'] = $link_img;
    $mUserCur['ProfileUrl'] = FRONT_SITE_URL.'user/profile/viewprofile?profile='.$mUserCur['user_id'];
    $mUserCur['ImInterestedUrl'] = FRONT_SITE_URL.'/v3/ImInterested/?profile='.$mUserCur['user_id'];
    $favorite = mUserFavorite::model()->find('user_id=:user_id and user_favorited=:user_favorited', 
                                        array(':user_id'=>user()->getUserProperty('user_id'),'user_favorited'=>$mUserCur['user_id']));
    if($favorite != false)
      $favorite = true;
    
    $like = mUserLike::model()->find('user_id=:user_id and user_id_liked=:user_id_liked', 
                                        array(':user_id'=>user()->getUserProperty('user_id'),'user_id_liked'=>$mUserCur['user_id']));
    if($like != false)
      $like = true;
    
    $mUserCur['IsFavorite'] = $favorite;
    $mUserCur['MediaId'] = '4855284';
    $mUserCur['IsLiked'] = $like;
    $mUserCur['EncodedMemberId'] = $mUserCur['user_id'];
    $mUserCur['SiteId'] = '21';    
    
    $result['PreviousMember'] = $mUserCur;
            
    $result['Status'] = 'Success';
    $result['Offset'] = $_POST['offset']+1;
    echo CJSON::encode($result);
    Yii::app()->end();
  }
  
  public function actionNext(){
    $result = array();
    if(!isset($_POST['offset'])){
      echo CJSON::encode($result);
      Yii::app()->end();
    }
    
    $find_gender = 'male';
    $mProfile = new mProfile();
    $profile = $mProfile->getProfile(user()->getUserProperty('user_id'));
    if($profile['gender']=='male')
      $find_gender = 'female';
    
    $mUser = new User();
    $mUserCur = $mUser->getUserToViewed($_POST['offset']+1, $find_gender);
    if($mUserCur==false){
      echo CJSON::encode($result);
      Yii::app()->end();
    }
    $link_img = '';
    if ($mUserCur['link_img'] != null) 
        $link_img = FRONT_SITE_URL.'www/uploads/photos/thumbs/thumb_'.$mUserCur['link_img']; 
    else {
        $link_img = $mUserCur['gender'] == 'male' ? FRONT_SITE_URL . 'www/images/images1/noPhoto-1.gif' : FRONT_SITE_URL . 'www/images/images1/noPhoto-2.gif';
    }
    
    $mUserCur['PhotoUrl'] = $link_img;
    $mUserCur['ProfileUrl'] = FRONT_SITE_URL.'user/profile/viewprofile?profile='.$mUserCur['user_id'];
    $mUserCur['ImInterestedUrl'] = FRONT_SITE_URL.'/v3/ImInterested/?profile='.$mUserCur['user_id'];
    $favorite = mUserFavorite::model()->find('user_id=:user_id and user_favorited=:user_favorited', 
                                        array(':user_id'=>user()->getUserProperty('user_id'),'user_favorited'=>$mUserCur['user_id']));
    if($favorite != false)
      $favorite = true;
    
    $like = mUserLike::model()->find('user_id=:user_id and user_id_liked=:user_id_liked', 
                                        array(':user_id'=>user()->getUserProperty('user_id'),'user_id_liked'=>$mUserCur['user_id']));
    if($like != false)
      $like = true;
    
    $mUserCur['IsFavorite'] = $favorite;
    $mUserCur['MediaId'] = '4855284';
    $mUserCur['IsLiked'] = $like;
    $mUserCur['EncodedMemberId'] = $mUserCur['user_id'];
    $mUserCur['SiteId'] = '21';    
    
    $result['CurrentMember'] = $mUserCur;
    
    
    $result['PreviousMember'] = null;
    
    
    $mUserCur = $mUser->getUserToViewed($_POST['offset']+2, $find_gender);
    if($mUserCur==false){
      echo CJSON::encode($result);
      Yii::app()->end();
    }
    $link_img = '';
    if ($mUserCur['link_img'] != null) 
        $link_img = FRONT_SITE_URL.'www/uploads/photos/thumbs/thumb_'.$mUserCur['link_img']; 
    else {
        $link_img = $mUserCur['gender'] == 'male' ? FRONT_SITE_URL . 'www/images/images1/noPhoto-1.gif' : FRONT_SITE_URL . 'www/images/images1/noPhoto-2.gif';
    }
    
    $mUserCur['PhotoUrl'] = $link_img;
    $mUserCur['ProfileUrl'] = FRONT_SITE_URL.'user/profile/viewprofile?profile='.$mUserCur['user_id'];
    $mUserCur['ImInterestedUrl'] = FRONT_SITE_URL.'/v3/ImInterested/?profile='.$mUserCur['user_id'];
    $favorite = mUserFavorite::model()->find('user_id=:user_id and user_favorited=:user_favorited', 
                                        array(':user_id'=>user()->getUserProperty('user_id'),'user_favorited'=>$mUserCur['user_id']));
    if($favorite != false)
      $favorite = true;
    
    $like = mUserLike::model()->find('user_id=:user_id and user_id_liked=:user_id_liked', 
                                        array(':user_id'=>user()->getUserProperty('user_id'),'user_id_liked'=>$mUserCur['user_id']));
    if($like != false)
      $like = true;
    
    $mUserCur['IsFavorite'] = $favorite;
    $mUserCur['MediaId'] = '4855284';
    $mUserCur['IsLiked'] = $like;
    $mUserCur['EncodedMemberId'] = $mUserCur['user_id'];
    $mUserCur['SiteId'] = '21';    
    
    $result['NextMember'] = $mUserCur;
            
    $result['Status'] = 'Success';
    $result['Offset'] = $_POST['offset']+1;
    echo CJSON::encode($result);
    Yii::app()->end();
  }
  
  public function actionSave(){
    
  }
  
  public function actionLike(){
    $result = array();
    $result['Status'] = 'Success';
    
    if(!isset($_POST['encodedMemberId'])){
      echo CJSON::encode($result);
      Yii::app()->end();
    }
    
    $mUserLike = new mUserLike();
    $checkExists = $mUserLike->find('user_id=:user_id and user_id_liked=:user_id_liked', array(':user_id' => user()->getUserProperty('user_id'), ':user_id_liked' => $_POST['encodedMemberId']));
    if ($checkExists == null) {
      $mUserLike->user_id = user()->getUserProperty('user_id');
      $mUserLike->user_id_liked = $_POST['encodedMemberId'];
      $mUserLike->show_recently_update = 1;
      $mUserLike->date_like = date('Y-m-d');
      if ($mUserLike->save()) {
        $result['Status'] = 'Success';
        $result['message'] = 'Saved to favorites!';  
      }else{

      }
    }else{
      echo 'abc1';
      $result['Status'] = 'Success';
      $result['message'] = 'Saved to favorites!';
    }
    echo CJSON::encode($result);
    Yii::app()->end();
  }
  
}